package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"time"
)

func main() {
	service := "127.0.0.1:5002"
	if len(os.Args) == 2 {
		service = os.Args[1]
	}

	fmt.Println("Connect service:", service)
	//获取socket
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	checkError(err)

	//拨号
	conn, err := net.DialTCP("tcp", nil, tcpAddr)
	checkError(err)

	_, err = conn.Write([]byte("1111"))
	checkError(err)

	for {

		data := make([]byte, 1025)
		r := bufio.NewReader(conn)
		r.Read(data)

		fmt.Println(string(data))
		time.Sleep(2 * time.Second)
		_, err = conn.Write([]byte("timestamp"))
		checkError(err)

	}

	os.Exit(0)
}

func checkError(err error) {
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fatal error:%s", err.Error())
		os.Exit(1)
	}

}
